//
//  SCLoginController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/5/29.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCLoginController.h"
#import "SCRegisterController.h"
#import "SCAlterPasswordController.h"
#import "SCCodeLoginController.h"

@interface SCLoginController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;

@end

@implementation SCLoginController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar lt_setBackgroundColor:BGColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"手机密码登录";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"back"] target:self action:@selector(back)];
}
- (void)back{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
//点击按钮
- (IBAction)clickBtns:(UIButton *)sender {
    if (sender.tag == 0) {//忘记密码
        SCAlterPasswordController *vc = [[SCAlterPasswordController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (sender.tag == 1){//短信验证码登录
        SCCodeLoginController *vc = [[SCCodeLoginController alloc]init];
        vc.loginSuccess = ^{
            [self back];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (sender.tag == 2){//注册
        SCRegisterController *vc = [[SCRegisterController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
//登录
- (IBAction)clickLoginBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
//    if (self.phoneTF.text.length != 11) {
//        [SVProgressHUD showImage:nil status:@"请输入正确的手机号码"];
//        return;
//    }
    if (self.pwdTF.text.length < 6 || self.pwdTF.text.length > 10) {
        [SVProgressHUD showImage:nil status:@"请输入正确位数的密码"];
        return;
    }
    NSDictionary *dict = @{@"phone":self.phoneTF.text,
                           @"pwd":self.pwdTF.text};
    NSString *url = [MyTool splicingWithUrl:@"login/app/index.php?i=2&c=entry&m=jack_sys&do=auth&op=autho&type=login_psw"];
    [SVProgressHUD showWithStatus:@"登录中..."];
    
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        NSLog(@"---%@",result);
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        NSUserDefaults *uf = [NSUserDefaults standardUserDefaults];
        [uf setObject:result[@"data"][@"newtoken"] forKey:@"token"];
        [uf setObject:self.phoneTF.text forKey:@"phone"];
        [uf synchronize];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SuccessLogin" object:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}

@end
